﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using AspNet.Identity.EF.BigIntId;
using AspNet.Identity.EF.BigIntId.Config;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace AspNetMVC_IDentity_Example.Models
{
    // 可以通过向 ApplicationUser 类添加更多属性来为用户添加配置文件数据。若要了解详细信息，请访问 http://go.microsoft.com/fwlink/?LinkID=317594。
    //public class ApplicationUser : IdentityUser
    //{
    //    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    //    {
    //        // 请注意，authenticationType 必须与 CookieAuthenticationOptions.AuthenticationType 中定义的相应项匹配
    //        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
    //        // 在此处添加自定义用户声明
    //        return userIdentity;
    //    }
    //}

    //public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    //{
    //    public ApplicationDbContext()
    //        : base("DefaultConnection", throwIfV1Schema: false)
    //    {
    //    }

    //    public static ApplicationDbContext Create()
    //    {
    //        return new ApplicationDbContext();
    //    }
    //}

    public class ApplicationUser : ApplicationUserBase
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser,long> manager)
        {
            // 请注意，authenticationType 必须与 CookieAuthenticationOptions.AuthenticationType 中定义的相应项匹配
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // 在此处添加自定义用户声明
            return userIdentity;
        }

        [Display(Name = "用户名")]
        [Required]
        [StringLength(50)]
        public override string UserName { get; set; }
        [Display(Name = "真实姓名")]
        public string RealName { get; set; }

        [Display(Name = "手机号码")]
        [StringLength(11)]
        public override string PhoneNumber { get; set; }
        [Display(Name = "创建时间")]
        public DateTime? CreateTime { get; set; }
        [Display(Name = "是否启用")]
        public bool IsEnable { get; set; }
    }

    public class ApplicationDbContext : AspNet.Identity.EF.BigIntId.ApplicationContextBase<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", GetTableConfig())
        {
        }

        private static AspNet.Identity.EF.BigIntId.Config.AspNetTableConfig GetTableConfig()
        {
            AspNetTableConfig config = new AspNetTableConfig();
            config.Add(AspNetIdentityTable.AspNetRoles, "Manage_Role");
            config.Add(AspNetIdentityTable.AspNetUserRoles, "Manage_UserRole");
            config.Add(AspNetIdentityTable.AspNetUserClaims, "Manage_UserClaim");
            config.Add(AspNetIdentityTable.AspNetUserLogins, "Manage_UserLogin");
            config.Add(AspNetIdentityTable.AspNetUsers, "Manage_User");
            return config;
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}